package com.myjpa.generate;

import lombok.Getter;
import lombok.Setter;

import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @author: yaoll
 * @date: 2020-09-04
 * @verison: 1.0
 */
@Getter
@Setter
public class GenerateContext {

	private String criteriaPackage;

	private String rootPath;
	private String path;
	private String pkg;
	private String className;
	private String mappingPath;
	private String tableName;
	private String name;
	private String entityClass;
	private String entityClassSuffix = "";
	private String modelClass;
	private String repositoryClass;
	private String repoName;
	private String serviceClass;
	private String controllerClass;
	private boolean overrideFile = false;


	private String responsePackage;

	private boolean withModel = false;

	private PrimaryKey primaryKey;
	private List<Attribute> atts;

	public void init() {
		this.name = this.className.substring(0, 1).toLowerCase() + this.className.substring(1);
		this.entityClass = this.className + this.entityClassSuffix;
		this.modelClass = this.className + "Model";
		this.repositoryClass = this.className + "Repository";
		this.serviceClass = this.className + "Service";
		this.controllerClass = this.className + "Controller";
		this.repoName = this.name + "Repository";
		this.initPath();
		if (this.primaryKey == null) {
			this.primaryKey = new PrimaryKey("id", "Integer", PrimaryKey.GENERATION_TYPE_IDENTITY);
		}
	}

	private void initPath() {
		StringBuilder pathBuilder = new StringBuilder(this.getRootPath());
		pathBuilder.append("/src/main/java");
		String pkg = this.getPkg();
		String[] split = pkg.split("\\.");
		for (String s : split) {
			pathBuilder.append("/").append(s);
		}
		this.path = pathBuilder.toString();
		System.out.println(this.path);
		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}

		List<String> subPaths = withModel ?
				Arrays.asList("/controller", "/entity", "/service", "/repository", "/model")
				: Arrays.asList("/controller", "/entity", "/service", "/repository");
		for (String subPath : subPaths) {
			file = new File(this.path + subPath);
			if (!file.exists()) {
				file.mkdirs();
			}
		}
	}

	public void clearAttributes() {
		atts = new LinkedList<>();
	}

	public void addAttribute(String name, Class type) {
		if (atts == null) {
			atts = new LinkedList<>();
		}
		if (this.primaryKey != null) {
			String keyName = this.primaryKey.getName();
			if (name.equalsIgnoreCase(keyName)) {
				return;
			}
		}
		atts.add(new Attribute(type, name));
	}

	@Getter
	@Setter
	public class PrimaryKey {

		public static final String GENERATION_TYPE_IDENTITY = "IDENTITY";
		public static final String GENERATION_TYPE_ASSIGNED = "ASSIGNED";

		private String name;
		private String type;
		private String generationType;

		public PrimaryKey(String name, String type, String generationType) {
			this.name = name;
			this.type = type;
			this.generationType = generationType;
		}
	}

	public void setPrimaryKey(String name, String type, String generationType) {
		this.primaryKey = new PrimaryKey(name, type, generationType);
	}
}
